package hello.leavesC.presenter.viewModel;

import androidx.lifecycle.MediatorLiveData;

import com.tencent.imsdk.TIMCallBack;
import com.tencent.imsdk.TIMGroupManager;
import com.tencent.imsdk.TIMGroupMemberInfo;
import com.tencent.imsdk.TIMGroupReceiveMessageOpt;
import com.tencent.imsdk.TIMValueCallBack;
import com.tencent.imsdk.ext.group.TIMGroupManagerExt;
import com.tencent.imsdk.ext.group.TIMGroupMemberResult;

import java.util.ArrayList;
import java.util.List;

import hello.leavesC.presenter.model.GroupMemberInfo;
import hello.leavesC.presenter.viewModel.base.BaseViewModel;


public class GroupProfileViewModel extends BaseViewModel {

    //单独群组成员的新的信息用一个接口的数组来存放
    private MediatorLiveData<TIMGroupMemberInfo> groupMemberInfoLiveData = new MediatorLiveData<>();
    //群组有哪一些群成员用一个list来存放
    private MediatorLiveData<List<GroupMemberInfo>> groupMemberInfoListLiveData = new MediatorLiveData<>();


    //获取群组成员的信息 第一步调用基类在窗口显示文字，然后创建一个数组用来加载要显示的成员信息，
    public void getGroupMemberInfo(String groupId, String identifier) {
        showLoadingDialog("正在获取群成员资料");
        List<String> identifierList = new ArrayList<>();
        identifierList.add(identifier);
        //内嵌函数，具体的实现获取某个群成员的资料，通过调用SDK接口把成员的信息进行setvalue
        TIMGroupManagerExt.getInstance().getGroupMembersInfo(groupId, identifierList, new TIMValueCallBack<List<TIMGroupMemberInfo>>() {
            @Override
            //失败的时候调用日志文件 ，在窗口显示错误提示
            public void onError(int i, String s) {
                dismissLoadingDialog();
                showToast(s);
            }
            //成功的时候就把信息给set装入我们的私有成员数组里
            @Override
            //把那个群成员和那个其在的群组传进来，类似微信看到群成员然后继续点进去看他的详细资料
            public void onSuccess(List<TIMGroupMemberInfo> groupMemberInfoList) {
                dismissLoadingDialog();
                if (groupMemberInfoList != null && groupMemberInfoList.size() > 0) {
                    groupMemberInfoLiveData.setValue(groupMemberInfoList.get(0));
                }
            }
        });
    }

    public void getGroupMembers(String groupId) {
        showLoadingDialog("正在获取群成员列表");
        TIMGroupManagerExt.getInstance().getGroupMembers(groupId, new TIMValueCallBack<List<TIMGroupMemberInfo>>() {
            @Override
            public void onError(int i, String s) {
                dismissLoadingDialog();
                showToast(s);
            }
//这个函数的功能是把在某个群的成员中的信息传到局部变量groupMemberInfoList中，然后成功的时候就把信息给set装入我们的私有成员数组里
            @Override
            public void onSuccess(List<TIMGroupMemberInfo> timGroupMemberInfoList) {
                dismissLoadingDialog();
                List<GroupMemberInfo> groupMemberInfoList = new ArrayList<>();
                //把在
                for (TIMGroupMemberInfo info : timGroupMemberInfoList) {
                    groupMemberInfoList.add(new GroupMemberInfo(info));
                }
                groupMemberInfoListLiveData.setValue(groupMemberInfoList);
            }
        });
    }



    //群号掺入函数几个进行退出群
    public void quitGroup(String groupId) {
        showLoadingDialog("正在退出群组");
        //群号掺入函数几个进行退出群
        TIMGroupManager.getInstance().quitGroup(groupId, new TIMCallBack() {
            @Override
            public void onError(int i, String s) {
                dismissLoadingDialog();
                showToast(s);
            }

            @Override
            public void onSuccess() {
                dismissLoadingDialog();
                showToast("已退出群组");
                finishWithResultOk();
            }
        });
    }

    public void modifyGroupReceiveMessageOpt(String groupId, String identifier, TIMGroupReceiveMessageOpt opt) {
        TIMGroupManagerExt.ModifyMemberInfoParam param = new TIMGroupManagerExt.ModifyMemberInfoParam(groupId, identifier);
        //设置接受群消息的模式，屏蔽，接受不提醒等选项
        param.setReceiveMessageOpt(opt);
        TIMGroupManagerExt.getInstance().modifyMemberInfo(param, new TIMCallBack() {
            @Override
            public void onError(int i, String s) {
                showToast(s);
            }

            @Override
            public void onSuccess() {

            }
        });
    }

    private void modifyProfile(String groupId, String groupName, String introduction, String notification) {
        showLoadingDialog("正在修改");
        //修改群组的信息，群组的修改有一些限制，群名称，群声明，群注意等不能为空
        TIMGroupManagerExt.ModifyGroupInfoParam param = new TIMGroupManagerExt.ModifyGroupInfoParam(groupId);
        if (groupName != null) {
            param.setGroupName(groupName);
        }
        if (introduction != null) {
            param.setIntroduction(introduction);
        }
        if (notification != null) {
            param.setNotification(notification);
        }
        TIMGroupManagerExt.getInstance().modifyGroupInfo(param, new TIMCallBack() {
            @Override
            public void onError(int code, String desc) {
                showToast(desc);
                dismissLoadingDialog();
            }

            @Override
            public void onSuccess() {
                showToast("修改成功");
                dismissLoadingDialog();
                finish();
            }
        });
    }

    //把群号传入即可修改
    public void modifyGroupName(String groupId, String groupName) {
        modifyProfile(groupId, groupName, null, null);
    }

    public void modifyGroupIntroduction(String groupId, String introduction) {
        modifyProfile(groupId, null, introduction, null);
    }

    public void modifyGroupNotification(String groupId, String notification) {
        modifyProfile(groupId, null, null, notification);
    }



    //邀请好友入群，把列表的好友以及群号传进来
    public void inviteGroup(String groupId, List<String> members) {
        showLoadingDialog("正在邀请好友入群");
        TIMGroupManagerExt.getInstance().inviteGroupMember(groupId, members, new TIMValueCallBack<List<TIMGroupMemberResult>>() {
            @Override
            public void onError(int i, String s) {
                dismissLoadingDialog();
                showToast(s);
            }

            @Override
            public void onSuccess(List<TIMGroupMemberResult> timGroupMemberResults) {
                dismissLoadingDialog();
                showToast("好友已入群");
                finish();
            }
        });
    }



    //创建群信息，把群组的名字，字符串类型，要邀请的好友传入
    public void createGroup(String name, String type, List<String> members) {
        showLoadingDialog("正在创建群组");
        List<TIMGroupMemberInfo> groupMemberInfoList = new ArrayList<>();
        //把新添加的信息加入群GroupMemberInfo中
        for (String member : members) {
            TIMGroupMemberInfo newMember = new TIMGroupMemberInfo(member);
            groupMemberInfoList.add(newMember);
        }
        //执行创建群的功能
        TIMGroupManager.CreateGroupParam groupGroupParam = new TIMGroupManager.CreateGroupParam(type, name);
        groupGroupParam.setMembers(groupMemberInfoList);
        TIMGroupManager.getInstance().createGroup(groupGroupParam, new TIMValueCallBack<String>() {
            @Override
            public void onError(int i, String s) {
                dismissLoadingDialog();
                showToast(s);
            }

            @Override
            public void onSuccess(String s) {
                dismissLoadingDialog();
                showToast("创建成功");
                finish();
            }
        });
    }

    public MediatorLiveData<TIMGroupMemberInfo> getGroupMemberInfoLiveData() {
        return groupMemberInfoLiveData;
    }

    public MediatorLiveData<List<GroupMemberInfo>> getGroupMemberInfoListLiveData() {
        return groupMemberInfoListLiveData;
    }

}